const { User } = require('../models');
const bcrypt = require('bcryptjs');

// 获取用户信息
exports.getUserInfo = async (req, res) => {
    try {
        const userId = req.user.userinfo.id;

        // 获取用户信息，不包含密码字段
        const user = await User.findById(userId)
            .select('-password')
            .populate('verification');

        if (!user) {
            return res.status(404).json({
                success: false,
                message: '用户不存在'
            });
        }

        res.status(200).json({
            success: true,
            data: {
                id: user._id,
                username: user.username,
                email: user.email,
                phone: user.phone,
                role: user.role,
                verification: user.verification
            }
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: '获取用户信息失败',
            error: error.message
        });
    }
};

// 修改密码
exports.changePassword = async (req, res) => {
    try {
        const { oldPassword, newPassword } = req.body;
        const userId = req.user.userinfo.id;

        // 获取用户信息
        const user = await User.findById(userId);
        if (!user) {
            return res.status(404).json({
                success: false,
                message: '用户不存在'
            });
        }

        // 验证原密码
        const isMatch = await bcrypt.compare(oldPassword, user.password);
        if (!isMatch) {
            return res.status(400).json({
                success: false,
                message: '原密码错误'
            });
        }

        // 加密新密码
        const salt = await bcrypt.genSalt(10);
        const hashedPassword = await bcrypt.hash(newPassword, salt);

        // 更新密码
        user.password = hashedPassword;
        await user.save();

        res.status(200).json({
            success: true,
            message: '密码修改成功'
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: '密码修改失败',
            error: error.message
        });
    }
};

// 修改用户信息
exports.updateUserInfo = async (req, res) => {
    try {
        const { email, phone } = req.body;
        const userId = req.user.userinfo.id;

        // 只更新提供的字段
        const updateData = {};
        if (email) updateData.email = email;
        if (phone) updateData.phone = phone;

        // 更新用户信息
        const user = await User.findByIdAndUpdate(
            userId,
            updateData,
            { new: true, runValidators: true }
        );

        if (!user) {
            return res.status(404).json({
                success: false,
                message: '用户不存在'
            });
        }

        res.status(200).json({
            success: true,
            message: '用户信息更新成功',
            data: {
                id: user._id,
                username: user.username,
                email: user.email,
                phone: user.phone,
                role: user.role
            }
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: '用户信息更新失败',
            error: error.message
        });
    }
}; 